using UnityEngine;
using QFramework;
using System;
using static UnityEngine.EventSystems.EventTrigger;
using QAssetBundle;

namespace ProjectSurvivor
{
	public partial class Enemy : ViewController,IEnemy
	{

		public float HP = 3;

		public float moveSpeed = 2f;

        public Color DissolveColor = Color.yellow;
		void Start()
		{
			EnemyGenerator.EnemyCount.Value++;
		}

        private void OnDestroy()
        {
            EnemyGenerator.EnemyCount.Value--;
        }

        private void FixedUpdate()
        {
            if (Player.Default)
            {
                var direction = (Player.Default.transform.position - transform.position).normalized;
                SelfRigibody2D.velocity = (direction * moveSpeed);
            }
            else
            {
                SelfRigibody2D.velocity=Vector3.zero;
            }
        }

        void Update()
		{
			if (HP <= 0)
			{
				Global.GeneratePowerUp(gameObject);
                FXController.Play(Triangle,DissolveColor);
                AudioKit.PlaySound(Sfx.ENEMYDIE);
				this.DestroyGameObjGracefully();
			}
		}

		private bool mIgnoreHurt=false;

        public void Hurt(float value,bool force=false)
        {
			if (mIgnoreHurt && !force) return;

            FloatingTextController.Play(transform.position + Vector3.up * 0.5f, value.ToString("0"));

            Triangle.color = Color.red;
			AudioKit.PlaySound("Hit");
            ActionKit.Delay(0.2f, () =>
            {
                this.HP -= value;
                this.Triangle.color = Color.white;
				mIgnoreHurt = false;
            }).Start(this);
        }

        public void SetHPScale(float hPScale)
        {
            HP*=hPScale;
        }

        public void SetSpeedScale(float speedScale)
        {
            moveSpeed*=speedScale;
        }
    }
}
